package com.micro.balance;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import org.springframework.stereotype.Component;

@Component("roundbinweight")
public class RoundbinWeightLoadBalance extends AbstractLoadBalance{
	private static int pos=0;//当前位置
	
	@Override
	protected String doSelect(Map<String, Integer> map) {
		ArrayList<String> lists = new ArrayList<String>();
		
		
		//根据权重来生成比重的数据放入List集合
		Iterator<String> iterator = map.keySet().iterator();
		while(iterator.hasNext()){
			String key=iterator.next();
			Integer weight=map.get(key);
			
			for(int i=0;i<weight;i++){
				lists.add(key);
			}
		}
		//开始轮询
		String result="";
    	synchronized (RoundbinLoadBalance.class) {
			if(pos>=lists.size()){
				pos=0;
			}
			result=lists.get(pos);
			pos++;
		}
        
        return result;
	}

}
